﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ENProyectoSIETE;
using CADProyectoSIETE.Properties;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace CADProyectoSIETE
{
    public class MisionCAD
    {
        public bool insertarMision(ENMision Mision)
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from Mision ", conex);

                da.Fill(bdvirtual, "mision");
                //Obtenemos la tabla Misiones
                DataTable tabla = new DataTable();
                tabla = bdvirtual.Tables["mision"];
                //Creamos la nueva fila a insertar
                DataRow nuevafila = tabla.NewRow();
                nuevafila[1] = Mision.NombreMision;
                nuevafila[2] = Mision.MapaMision;
                nuevafila[3] = Mision.DescripcionMision;
                nuevafila[4] = Mision.InicioMision;
                nuevafila[5] = Mision.ObjetivoMision;
                nuevafila[6] = Mision.DificultadMision;
                nuevafila[7] = Mision.TabernaMision;
                nuevafila[8] = Mision.ObjetivoMision;

                tabla.Rows.Add(nuevafila);

                SqlCommandBuilder cbuilder = new SqlCommandBuilder(da);
                da.Update(bdvirtual, "mision");

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conex.Close();
            }

            conex.Close();
            return true;
        }

        public bool borrarMision(ENMision mision)
        {
            int i = 0;
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from Mision ", conex);

                da.Fill(bdvirtual, "mision");
                //Obtenemos la tabla enemigos
                DataTable tabla = new DataTable();
                tabla = bdvirtual.Tables["mision"];

                foreach (DataRow registro in tabla.Rows)
                {
                    if ((int)registro["idMision"] == mision.IdMision)
                    {
                        tabla.Rows[i].Delete();
                    }
                    i++;
                }

                SqlCommandBuilder cbuilder = new SqlCommandBuilder(da);
                da.Update(bdvirtual, "mision");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conex.Close();
            }

            conex.Close();
            return true;
        }

        public ENMision obtenerMision(int index)
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from Mision where idMision=" + index.ToString(), conex);

                da.Fill(bdvirtual, "mision");
                ENMision mision = new ENMision();

                foreach (DataRow registro in bdvirtual.Tables["mision"].Rows)
                {
                    mision.IdMision = int.Parse(registro[0].ToString());
                    mision.NombreMision = registro[1].ToString();
                    mision.MapaMision = int.Parse(registro[2].ToString());
                    mision.DescripcionMision = registro[3].ToString();
                    mision.InicioMision = int.Parse(registro[4].ToString());
                    mision.ObjetivoMision = int.Parse(registro[5].ToString());
                    mision.DificultadMision = int.Parse(registro[6].ToString());
                    if (registro[7] != null)
                        mision.TabernaMision = int.Parse(registro[7].ToString());
                    else
                        mision.TabernaMision = -1;
                }

                return mision;
            }
            catch
            {
                ENMision misionerror = new ENMision();
                return misionerror;
            }
            finally
            {
                conex.Close();
            }
        }

        public DataTable obtenerMisionNombre(string nombre, string nom_des)
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = null;
                if (nom_des == "nombre")
                {
                    da = new SqlDataAdapter("select * from mision where nombreMision LIKE '%" + nombre + "%'", conex);
                }
                else if (nom_des == "descripcion")
                {
                    da = new SqlDataAdapter("select * from mision where descripcionMision LIKE '%" + nombre + "%'", conex);
                }
                da.Fill(bdvirtual, "mision");
                DataTable tabla = new DataTable();
                tabla = bdvirtual.Tables["mision"];

                return tabla;
            }
            catch
            {
                DataTable tablavacia = new DataTable();
                return tablavacia;
            }
            finally
            {
                conex.Close();
            }
        }

        public bool editarMision(ENMision mision)
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);
            bool editado = false;

            try
            {
                int i = 0;
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from Mision", conex);

                da.Fill(bdvirtual, "mision");
                DataTable tabla = new DataTable();
                tabla = bdvirtual.Tables["mision"];

                foreach (DataRow registro in tabla.Rows)
                {
                    if ((int)registro["idMision"] == mision.IdMision)
                    {
                        tabla.Rows[i][0] = mision.IdMision;
                        tabla.Rows[i][1] = mision.NombreMision;
                        tabla.Rows[i][2] = mision.MapaMision;
                        tabla.Rows[i][3] = mision.DescripcionMision;
                        tabla.Rows[i][4] = mision.InicioMision;
                        tabla.Rows[i][5] = mision.ObjetivoMision;
                        tabla.Rows[i][6] = mision.DificultadMision;
                        tabla.Rows[i][7] = mision.TabernaMision;
                        tabla.Rows[i][8] = mision.ObjetivoMision;
                        editado = true;
                    }
                    i++;
                }

                SqlCommandBuilder cbuilder = new SqlCommandBuilder(da);
                da.Update(bdvirtual, "mision");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conex.Close();
            }

            conex.Close();
            return editado;
        }

        public DataSet obtenerRecompensas(int index)
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from recompensa where idRecompensa in (Select idRecompensa from MisionRecompensa where idMision =" + index.ToString() + ")", conex);

                da.Fill(bdvirtual, "recompensa");
                return bdvirtual;
            }
            catch (Exception ex)
            {
                DataSet bdvacia = new DataSet();
                throw ex;
            }
        }

        public DataSet obtenerMisiones()
        {
            string cadenaConexion = Settings.Default.BDProyecto7ConnectionString;
            SqlConnection conex = new SqlConnection(cadenaConexion);

            try
            {
                DataSet bdvirtual = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from mision", conex);

                da.Fill(bdvirtual, "mision");
                return bdvirtual;
            }
            catch (Exception ex)
            {
                DataSet bdvacia = new DataSet();
                throw ex;
            }
        }
    }
}
